﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.Common.Helpers
{
    /// <summary>
    /// 字符串操作帮助类
    /// </summary>
    public class StringHelper : Framework.Interfaces.ISingletonDependency
    {
        /// <summary>
        /// 通过Helper类来确定字符串的长度
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int GetStringLength(string str)
        {
            str.MustNotNull();

            return Encoding.Default.GetBytes(str).Length;
        }

        /// <summary>
        /// 判断字符串中由制定字符分割的字符串是否包含制定的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="stringArray"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
        public static bool IsCompriseStr(string str, string stringArray, string strSplit)
        {
            if (CheckIsNullHelper<string>.CheckIsNullOrEmpty(stringArray))
            {
                return false;
            }
            string[] strArr = SplitString(stringArray.ToLower(), strSplit);
            for (int i = 0; i < strArr.Length; i++)
            {
                if (str.IndexOf(strArr[i]) > -1)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 分割字符串 如果分隔符为Null 则默认用空格代替
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
        public static string[] SplitString(string strContent, string strSplit)
        {
            if (strSplit.IsNullOrEmpty())
            {
                //如果分隔符为Null 则用空格表示 默认为空格
            }
            if (strContent.IsNullOrEmpty() == false)
            {
                return new string[0] { };
            }
            else
            {
                ///如果字符串不为Null
                if (strContent.IndexOf(strSplit) < 0)
                {
                    return new string[] { strContent };
                }
                else
                {
                    return RegexHelper.SplitString(strContent, strSplit, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                }
            }
        }

        /// <summary>
        /// 获取制定长度的分割后的字符串数组
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="strSplit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string[] SplitString(string strContent, string strSplit, int count)
        {
            if (count <= 0)
            {
                throw new Framework.Exceptions.BaiTuCoreException("分割字符串得到的结果不能小于等于0");
            }

            string[] result = new string[count];
            string[] strSplited = SplitString(strContent, strSplit);
            for (int i = 0; i < count; i++)
            {
                if (i < strSplited.Length)
                {
                    result[i] = strSplited[i];
                }
                else
                {
                    result[i] = string.Empty;
                }
            }
            return result;
        }

        /// <summary>
        /// 删除字符串尾部的回车/换行/空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RTrim(string str)
        {
            str.IsNullOrEmptyThrowException();
            for (int i = str.Length; i >= 0; i--)
            {
                if (str[i].Equals(" ") || str[i].Equals("\r") || str[i].Equals("\n"))
                {
                    str.Remove(str[i]);
                }
            }
            return str;
        }
        /// <summary>
        /// 清除给定字符串中的回车及换行符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ClearBR(string str)
        {
            return RegexHelper.ClearBR(str);
        }
        /// <summary>
        /// 自定义的替换字符串函数
        /// </summary>
        public static string ReplaceString(string sourceString, string searchString, string replaceString, bool IsCaseInsensetive)
        {
            return RegexHelper.ReplaceString(sourceString, searchString, replaceString, IsCaseInsensetive);
        }
        /// <summary>
        /// 判断是否为base64字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsBase64String(string str)
        {
            str.IsNullOrEmptyThrowException("判断是否为base64字符串,字符串为Null或Empty");
            //A-Z, a-z, 0-9, +, /, =
            return RegexHelper.IsBase64String(str);
        }
        /// <summary>
        /// 检测是否有Sql危险字符
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsSafeSqlString(string str)
        {
            str.IsNullOrEmptyThrowException("检测是否有Sql危险字符,sql为Null或Empty");
            return RegexHelper.IsSafeSqlString(str);
        }
        /// <summary>
        /// 检测是否有危险的可能用于链接的字符串
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsSafeUserInfoString(string str)
        {
            str.IsNullOrEmptyThrowException("检测是否有危险的可能用于链接的字符串,字符串为Null或Empty");
            return RegexHelper.IsSafeUserInfoString(str);
        }
        /// <summary>
        /// 清理字符串
        /// </summary>
        public static string CleanInput(string strIn)
        {
            strIn.IsNullOrEmptyThrowException("清理字符串,字符串为Null");
            return RegexHelper.CleanInput(strIn);
        }
        /// <summary>
        /// 删除最后一个字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ClearLastChar(string str)
        {
            return (str == "") ? "" : str.Substring(0, str.Length - 1);
        }

        /// <summary>
        /// 字符串如果操过指定长度则将超出的部分用指定字符串代替
        /// </summary>
        /// <param name="p_SrcString">要检查的字符串</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
        {
            return GetSubString(p_SrcString, 0, p_Length, p_TailString);
        }
        /// <summary>
        /// 取指定长度的字符串
        /// </summary>
        /// <param name="p_SrcString">要检查的字符串</param>
        /// <param name="p_StartIndex">起始位置</param>
        /// <param name="p_Length">指定长度</param>
        /// <param name="p_TailString">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
        {
            string myResult = p_SrcString;

            Byte[] bComments = Encoding.UTF8.GetBytes(p_SrcString);
            foreach (char c in Encoding.UTF8.GetChars(bComments))
            {    //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
                if ((c > '\u0800' && c < '\u4e00') || (c > '\xAC00' && c < '\xD7A3'))
                {
                    //if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") || System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))
                    //当截取的起始位置超出字段串长度时
                    if (p_StartIndex >= p_SrcString.Length)
                        return "";
                    else
                        return p_SrcString.Substring(p_StartIndex,
                                                       ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
                }
            }

            if (p_Length >= 0)
            {
                byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);

                //当字符串长度大于起始位置
                if (bsSrcString.Length > p_StartIndex)
                {
                    int p_EndIndex = bsSrcString.Length;

                    //当要截取的长度在字符串的有效长度范围内
                    if (bsSrcString.Length > (p_StartIndex + p_Length))
                    {
                        p_EndIndex = p_Length + p_StartIndex;
                    }
                    else
                    {   //当不在有效范围内时,只取到字符串的结尾

                        p_Length = bsSrcString.Length - p_StartIndex;
                        p_TailString = "";
                    }

                    int nRealLength = p_Length;
                    int[] anResultFlag = new int[p_Length];
                    byte[] bsResult = null;

                    int nFlag = 0;
                    for (int i = p_StartIndex; i < p_EndIndex; i++)
                    {
                        if (bsSrcString[i] > 127)
                        {
                            nFlag++;
                            if (nFlag == 3)
                                nFlag = 1;
                        }
                        else
                            nFlag = 0;

                        anResultFlag[i] = nFlag;
                    }

                    if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
                        nRealLength = p_Length + 1;

                    bsResult = new byte[nRealLength];

                    Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);

                    myResult = Encoding.Default.GetString(bsResult);
                    myResult = myResult + p_TailString;
                }
            }

            return myResult;
        }

    }
}
